package com.android.home.fragment;

import android.os.Bundle;
import android.view.ViewGroup;
import com.android.common.base.BaseEvent;
import com.android.common.base.rv.BaseRvAdapter;
import com.android.common.base.rv.BaseRvFragment;
import com.android.common.base.rv.BaseRvHolder;
import com.android.common.constant.Constant;
import com.android.common.constant.IntentKey;
import com.android.common.db.AssetsDBOperation;
import com.android.common.utils.Router;
import com.android.home.BuildConfig;
import com.android.home.R;
import com.android.home.adapter.holder.MsgNoticeEnterHolder;
import com.android.home.adapter.holder.MsgNoticeSwitchHolder;
import com.android.home.adapter.holder.MyInfoHolder;
import com.android.home.mvp.model.home.HomeThirdBean;
import com.android.home.mvp.present.home.HomePresent;
import com.android.common.utils.WebViewJsBridge;
import java.util.List;
import org.greenrobot.eventbus.Subscribe;

public class MyFragment extends BaseRvFragment<HomeThirdBean> {

    private static MyFragment instance;
    BaseRvAdapter<HomeThirdBean> mAdapter;
    private HomePresent present;

    public MyFragment() {
    }

    public static MyFragment getInstance() {
        if (null == instance) {
            instance = new MyFragment();
        }
        return instance;
    }

    @Override
    public int onSetRootViewId() {
        return super.onSetRootViewId();
    }

    @Override
    protected void onInitView() {
        super.onInitView();
        present = new HomePresent(this);
        //错误页面距上边缘的距离
        mRootView.post(() -> setErrorViewTopMargin(0));
    }


    @Override
    protected BaseRvAdapter<HomeThirdBean> initAdapter() {
        mAdapter = new BaseRvAdapter(this.getContext()) {

            @Override
            protected BaseRvHolder createDataViewHolder(ViewGroup parent, int viewType) {
                BaseRvHolder baseHolder = null;
                switch (viewType) {
                    case MyInfoHolder.TYPE_MY_INFO:
                        baseHolder = new MyInfoHolder(mLayoutInflater.inflate(R.layout.item_my_info, parent, false));
                        break;
                    case MsgNoticeEnterHolder.TYPE_MSG_NOTICE_ENTER:
                        baseHolder = new MsgNoticeEnterHolder(mLayoutInflater.inflate(R.layout.item_msg_notice_enter, parent, false));
                        break;
                    case MsgNoticeSwitchHolder.TYPE_MSG_NOTICE_SWITCH:
                        baseHolder = new MsgNoticeSwitchHolder(mLayoutInflater.inflate(R.layout.item_msg_notice_switch, parent, false));
                        baseHolder.setSwitchChangedListener((position, isChecked) -> {

                        });
                        break;
                }
                return baseHolder;
            }

            @Override
            public int getMultiplyItemViewType(int position) {
                int type =  MyInfoHolder.TYPE_MY_INFO;
                switch (position) {
                    case Constant.ZERO:
                        type = MyInfoHolder.TYPE_MY_INFO;
                        break;
                    case Constant.ONE:
                    case Constant.TWO:
                    case Constant.THREE:
                        type = MsgNoticeEnterHolder.TYPE_MSG_NOTICE_ENTER;
                        break;
                    case Constant.FOUR:
                    case Constant.FIFE:
                        type = MsgNoticeSwitchHolder.TYPE_MSG_NOTICE_SWITCH;
                        break;
                }
                return type;
            }
        };
        mAdapter.setOnItemClickListener((view, position) -> {
            // 1:跳转消息通知
            if (Constant.ONE == position) {
                //http://androidxref.com/9.0.0_r3/xref/packages/apps/Settings/src/com/android/settings/search/ResultPayloadUtils.java
                Bundle bundle = new Bundle();
                bundle.putBoolean(Constant.TOOLBAR_KEY,true);
                bundle.putParcelable(IntentKey.JS_BRIDGE_KEY,new WebViewJsBridge());
                bundle.putString(IntentKey.URL_KEY,"file:///android_asset/JsTest.html");
                Router.getInstance().startActivity(getActivity(), bundle, BuildConfig.WEB_VIEW);
            }else if (Constant.TWO == position) {
                Router.getInstance().startActivity(getActivity(), null, BuildConfig.FUNCTION_TEST);
                //AssetsDBOperation.getInstance(mActivity).openWritableDatabase();
            }else if (Constant.THREE == position) {
                Router.getInstance().startActivity(getActivity(), null, BuildConfig.SWITCH_ENV);
            }
        });
        return mAdapter;
    }

    @Override
    protected void onLoadData() {
        super.onLoadData();
        present.getMyInfoList();
    }

    @Override
    public<D> void onSucceed(D d, String methodName) {
        super.onSucceed(d, methodName);
        if (d instanceof List){
            List<HomeThirdBean> homeThirdBeans = (List<HomeThirdBean>)d;
            refreshOrLoadSuccess(homeThirdBeans);
        }

    }

    @Override
    protected String onSetTitleText() {
        return getString(R.string.my);
    }

    @Override
    protected boolean onNeedBack() {
        return false;
    }

    @Override
    public boolean onEnableLoadMore() {
        return false;
    }

    @Override
    protected boolean onNeedNetworkListener() {
        return true;
    }

    @Subscribe
    public void onEventMainThread(BaseEvent event) {
        if (BaseEvent.LOGIN.equals(event.getMsg())) {
            Router.getInstance().startActivity(getContext(), null, BuildConfig.LOGIN);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        instance = null;
    }
}
